package com.rainyun.rd.biz;

import com.rainyun.rc.command.annotation.BeanCmdBiz;
import com.rainyun.rc.command.annotation.CmdLine;
import com.rainyun.rc.config.NodeConfig;
import com.rainyun.rc.ioc.Beanzy;
import com.rainyun.rc.orm.RainDB;
import com.rainyun.rcmc.bukkit.annotation.Buk;
import com.rainyun.rd.common.BeanRd;
import com.rainyun.rd.common.RdCmdAttr;
import com.rainyun.rd.common.RdGlobal;
import com.rainyun.rd.common.RdPerm;
import com.rainyun.rd.config.RdLang;
import com.rainyun.rd.entity.PlayerInfo;
import com.rainyun.rd.util.RdNmsUtil;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.sql.SQLException;

/**rd指令业务类*/
@Buk
@BeanCmdBiz("raindemo")
public class BizRD {

	@Beanzy
    BizCommon bizCommon;
	@Beanzy
	RdGlobal global;
	//配置管理器
	@BeanRd(BeanRd.BeansRd.NodeConf)
	NodeConfig<?> ncf;

	@BeanRd(BeanRd.BeansRd.NmsUitl)
	RdNmsUtil nmsUtil;

	@BeanRd(BeanRd.BeansRd.DBMan)
	RainDB db;

	@CmdLine("test_tp <player>")
	@RdCmdAttr(order = 10, async = true,
			help = "§a /rd test_tp <玩家名>" +
					"\n&7 tp到玩家",
			perm = RdPerm.RD_Admin,
			ingame = true
	)
	public void tpPlayer(Player sd,Player p) {//智能转型
		sd.teleport(p.getLocation());
		//这里只是演示,语言获取过程忽略
		bizCommon.sendWithPrefix(sd,"芜湖!");
	}

	@CmdLine("player")
	@RdCmdAttr(order = 20,
			perm = RdPerm.RD_Player,
			help = "§a /rd player" +
					"\n&7 玩家信息操作"
	)
	String playerPath;//这个字段意义是为了对player节点赋予属性

	@CmdLine("player getinfo <player>")
	@RdCmdAttr(order = 21,
			help = "§a /rd player getinfo <玩家名>" +
					"\n&7 获取玩家信息",
			tab = "p_info"
	)
	public void getPlayerInfo(CommandSender sd,String name) throws SQLException {
		PlayerInfo info = db.newSql(PlayerInfo.class).find(name);
		if(info!=null){
			sd.sendMessage(info.toString());
		}else{
			bizCommon.sendWithPrefix(sd, "没有这样的玩家");
		}
	}
	@CmdLine("player setinfo <player> [age]:18")
	@RdCmdAttr(order = 22,
			help = "§a /rd player setinfo <玩家名> [年龄(默认18)]" +
					"\n&7 添加/更新玩家信息",
			tab = "p_info"
	)
	public void setPlayerInfo(CommandSender sd,String name,Integer age) throws SQLException {
		//age请使用包装类型
		PlayerInfo info = new PlayerInfo();
		info.setName(name);
		info.setAge(age);
		db.newSql(PlayerInfo.class).save(info);
		bizCommon.sendWithPrefix(sd,ncf.get(RdLang.biz_success));
	}

	@CmdLine("reload")
	@RdCmdAttr(order = 30, async = false,
			help = "§a /rd reload" +
					"\n&7 重载插件",
			perm = RdPerm.RD_Admin
	)
	public void reload(CommandSender sd) {
		if(global.BukReload())
			bizCommon.sendWithPrefix(sd, ncf.get(RdLang.reload_success));
		else
			bizCommon.sendWithPrefix(sd, ncf.get(RdLang.reload_error));
	}

	@CmdLine("testnms")
	@RdCmdAttr(order = 40,
			help = "§a /rd testnms" +
					"\n&7 显示手中物品nbt",
			ingame = true
	)
	public void getCurrentItemNBT(Player sd){
		ItemStack item = sd.getInventory().getItemInHand();
		if(item!=null&&!item.getType().equals(Material.AIR)){
			String tagstr = nmsUtil.getNBT(item);
			sd.sendMessage(tagstr);
		}else{
			//这里只是演示,语言获取过程忽略
			bizCommon.sendWithPrefix(sd,"手空空");
		}
	}

}









